/*
 * iShopPlace Project.
 */
package ishopplace.model.service.impl;

import ishopplace.model.businessobject.Account;
import ishopplace.model.dao.CustomerDao;
import ishopplace.model.exception.iShopPlaceException;
import ishopplace.model.service.CustomerService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * The implementation of the <code>CustomerService</code>.
 * <p>
 * Spring Framework is used to manage this service bean.
 * Since this class is not dependend on Spring API, 
 * it can be used outside the Spring IOC container.
 * 
 * @author <a href="mailto:derek@derekshen.com">Derek Y. Shen</a>
 * @see CustomerService
 */
public class CustomerServiceImpl implements CustomerService {
	//the logger for this class
	private Log logger = LogFactory.getLog(this.getClass());
	
	//the CatalogDao used
	private CustomerDao customerDao;
	
	/**
	 * Set the <code>CustomerDao</code>.
	 * <p>
	 * It can be used by the Spring IOC container.
	 * 
	 * @param newCustomerDao the CustomerDao to be set
	 */
	public void setCustomerDao(CustomerDao newCustomerDao) {
		this.customerDao = newCustomerDao;
	}
	
	public Account getAccount(String accountId) throws iShopPlaceException {
		try {
			return this.customerDao.getAccount(accountId);
		} catch (Exception e) {
			String msg = "Could not get account " + e.toString();
			this.logger.error(msg, e);
			throw new iShopPlaceException(msg, e);
		}
	}
	
	public Account signon(String username, String password) throws iShopPlaceException {
		Account account = this.getAccount(username);
		
		if (account != null) {
			if (password.equals(account.getPassword())) {
				return account;
			}
		}
		
		return null;
	}
	
	public void saveAccount(Account account) throws iShopPlaceException {
		try {
			this.customerDao.saveAccount(account);
		} catch (Exception e) {
			String msg = "Could not save account " + e.toString();
			this.logger.error(msg, e);
			throw new iShopPlaceException(msg, e);
		}
	}
	
	public void updateAccount(Account account) throws iShopPlaceException {
		try {
			this.customerDao.updateAccount(account);
		} catch (Exception e) {
			String msg = "Could not update account " + e.toString();
			this.logger.error(msg, e);
			throw new iShopPlaceException(msg, e);
		}
	}
}
